// Import("utils.js")
// Import("ocr.js")
// agent.shell('dumpsys battery set status 1')
// SystemPrint.log(app.getTopPackName())

var APP_GAMES = [
    'com.swyyzwztt11.swyyz',
    'com.gf.p6cncn.hwyad.hwy',
    'com.ztgame.yszt',
    'com.tuyoo.fish3d.jinritoutiao', // 捕鱼大作战
    'com.youkushengshi.yldrykthree', //鱼乐达人
    'com.tuyoo.fishue4.flb', // 途游休闲捕鱼
]

var threadGame = new thread();
function startGame_thread(onInit, onStep) {
    threadGame.runJsCode(() => {
        onInit()

        whilestep((steptimes) => {
            onStep(steptimes)
        }, (isEnableSwitch('moregametime') ? getRandomNum(60, 90) : getRandomNum(15, 20)) * 60, 5)
    }, "游戏线程")
}

var thread梦幻西游 = new thread();
function startGame_梦幻西游() {
    thread梦幻西游.runJsCode(() => {
        // 等待主界面
        ocrWaitForText('登录', 10)
        agentOrHidClick(ocrTextMoreComplete('登录游戏', '登录', '下次再说', '允许', '下次'), 3)
        agentOrHidClick(ocrTextMoreComplete('下次再说', '允许', '登录游戏', '登录'))
        ocrWaitForMoreTextMillisecond('公告', '版本号', '协议', '允许', 10)
        ocrWaitForMoreTextMillisecond('登录游戏', '登录', 20)
        agentOrHidClick(ocrTextMoreComplete('确定'))
        agentOrHidClick(ocrTextMoreComplete('登录游戏', '登录'))

        whilestep((steptimes) => {
            if (steptimes % 5 == 0) {
                agentOrHidClick(ocrTextMoreComplete('登录游戏', '登录', '确定', '下次再说', '允许'))
                agentOrHidClick(ocrTextMoreComplete('下次再说', '允许', '登录游戏', '登录',))
            }
            // ocrTextTrue('主线')
            var main主线 = ocrTextMoreInRegion(0.4, 0, 1, 1, '前往', '主线')
            if (main主线) {
                agentOrHidClick(main主线)
            } else {
                agentOrHidClickOffectY(ocrTextMoreInRegion(0.4, 0, 1, 1, '任务', '师门', '队伍'), -90)
            }


            var detects加速 = opencv.findImagesEx('图色848684.cv');
            if (detects加速 != null) {
                agentOrHidClick(detects加速[0])
            }

            var detects加速2 = opencv.findImagesEx('图色792449.cv');
            if (detects加速2 != null) {
                agentOrHidClick(detects加速2[0])
            }

            agentOrHidClick(ocrTextMoreInRegion(0.4, 0, 1, 1, '使用', '领取', '确认', '手动', '参加', '继续任务', '点击任意地方'))
            agentOrHidClick(ocrTextMoreComplete('购买', '上交', '继续任务',))

            // ocrTextTrue('选择要做的事')
            agentOrHidClickOffectY(ocrText('选择要做的事', '选择要', '要做的事', '请选择'), -90)

            var ocr1close = opencv.OCREx('图色459952.cv');
            if (ocr1close != null) {
                if (ocr1close.getAllString() == 'x') {
                    agentOrHidClick(ocr1close.getAllDetect()[0])
                }
            }

        }, (isEnableSwitch('moregametime') ? getRandomNum(60, 90) : getRandomNum(15, 20)) * 60)
    }, "梦幻西游线程")
}

var thread征途2 = new thread();
function startGame_征途2() {
    thread征途2.runJsCode(() => {

        // ocrTextTrue('进入游戏')
        ocrWaitForMoreTextMillisecond('征途', '公告', '踏上', 20)
        click(0.5, 0.95)
        agentOrHidClick(ocrText('征途', '踏上', '征', '上'))


        function closex(maxtimes) {
            if (maxtimes < 0) {
                return
            }


            var ocr1closex = opencv.OCREx('图色693470.cv');
            if (ocr1closex != null && ocr1closex.getAllDetect()) {
                agentOrHidClick(ocr1closex.getAllDetect()[0])
                closex(maxtimes - 1)
            }
        }

        whilestep((steptimes) => {
            if (steptimes % 20 == 0) {
                agentOrHidClick(ocrText('征途', '踏上', '征', '上'))

                closex(2)
            }

            if (steptimes % 5 == 0) {
                agentOrHidClick(ocrText('继续任务', '继续', '完成'))
            }

        }, (isEnableSwitch('moregametime') ? getRandomNum(10, 15) : getRandomNum(5, 15)) * 60, 5)
    }, "征途2线程")
}

var thread伏魔记 = new thread();
function startGame_伏魔记() {
    thread伏魔记.runJsCode(() => {

        // ocrTextTrue('进入游戏')
        ocrWaitForMoreTextMillisecond('进入游', '游戏公告', 20)
        click(0.5, 0.95)
        agentOrHidClick(ocrText('领取奖励', '点击屏幕', '开始', '进入游', '入游'))


        function closex(maxtimes) {
            if (maxtimes < 0) {
                return
            }


            var ocr1closex = opencv.OCREx('图色693470.cv');
            if (ocr1closex != null && ocr1closex.getAllDetect()) {
                agentOrHidClick(ocr1closex.getAllDetect()[0])
                closex(maxtimes - 1)
            }
        }

        whilestep((steptimes) => {
            if (steptimes % 20 == 0) {
                agentOrHidClick(ocrText('进入游', '入游', '开始', '签到', '下次'))

                closex(2)
            }

            if (steptimes % 5 == 0) {
                agentOrHidClick(ocrText('点击界', '点击屏幕', '确定'))

                clickOffectY(0.5, 1, 100)
            }

        }, (isEnableSwitch('moregametime') ? getRandomNum(10, 15) : getRandomNum(5, 15)) * 60, 5)
    }, "伏魔记线程")
}


function hangup_挂游戏() {
    hideLogWindow(1)
    // agentOrHidHome()

    printinfoOld('开始hangup_挂游戏')
    var isuccessPlay = false
    // 玩捕鱼
    if (!hasExcuteFunc('捕鱼') && getExcuteFuncTimes('hangup_挂游戏times') < 3) {
        agentOrHidHome()
        sleep.millisecond(500)

        if (startApp('com.hermes.p7buyu') || startApp('com.hermes.p7by')) {
            isuccessPlay = true
            addExcuteFuncTimes('hangup_挂游戏times')

            function closex(maxtimes) {
                if (maxtimes < 0) {
                    return
                }
                var ocr1closex = opencv.OCREx('图色253740.cv');
                if (ocr1closex != null && ocr1closex.getAllDetect()) {
                    agentOrHidClick(ocr1closex.getAllDetect()[0])
                    closex(maxtimes - 1)
                }
            }

            startGame_thread(
                () => {
                    ocrWaitForText('领取奖励', 10)
                }, (steptimes) => {
                    if (steptimes % 5 == 0) {
                        agentOrHidClick(ocrText('领取奖励', '点击屏幕', '开始', '签到'))

                        closex(8)
                    }

                    click(0.5, 0.5, 200)
                    click(0.5, 0.5, 0)
                }
            )

            sleep.second((isEnableSwitch('moregametime') ? getRandomNum(60, 90) : getRandomNum(15, 20)) * 60)
            if (threadGame && threadGame.isAlive()) {
                threadGame.stop()
            }

            setExcuteFunc('捕鱼')
            setExcuteFuncInTime一段时间内不再执行('hangup_挂游戏', 3 * 60 * 60)
            stopAllApp(true)
            agentOrHidHome()
            sleep.second(1)
        }


    }

    //梦幻西游
    // if (getExcuteFuncTimes('hangup_挂游戏times') < 3) {
    //     agentOrHidHome()
    //     sleep.millisecond(500)
    //     //找色返回目标数组
    //     if (startApp('com.netease.my.mj') || startApp('com.netease.my.yyxmjm')) {
    //         isuccessPlay = true
    //         addExcuteFuncTimes('hangup_挂游戏times')

    //         startGame_梦幻西游()

    //         sleep.second((isEnableSwitch('moregametime') ? getRandomNum(60, 90) : getRandomNum(15, 20)) * 60)
    //         if (thread梦幻西游 && thread梦幻西游.isAlive()) {
    //             thread梦幻西游.stop()
    //         }

    //         setExcuteFuncInTime一段时间内不再执行('hangup_挂游戏', 6 * 60 * 60)
    //     }

    //     stopAllApp(true)
    // }

    // SystemPrint.log(app.getTopPackName())
    // if (getExcuteFuncTimes('hangup_挂游戏times') < isEnableSwitch('moregametime') ? 5 : 3) {
    //     agentOrHidHome()
    //     sleep.millisecond(500)
    //     //找色返回目标数组
    //     if (startApp('demo')) {
    //         isuccessPlay = true
    //         addExcuteFuncTimes('hangup_挂游戏times')

    //         startGame_thread(
    //             () => {
    //                 ocrWaitForText('领取奖励', 10)
    //             }, (steptimes) => {
    //                 if (steptimes % 5 == 0) {
    //                     agentOrHidClick(ocrText('领取奖励', '点击屏幕', '开始', '签到'))
    //                 }
    //             }
    //         )

    //         sleep.second((isEnableSwitch('moregametime') ? getRandomNum(60, 90) : getRandomNum(15, 20)) * 60)
    //         if (threadGame && threadGame.isAlive()) {
    //             threadGame.stop()
    //         }

    //         setExcuteFuncInTime一段时间内不再执行('hangup_挂游戏', 9 * 60 * 60)

    //     }
    //     stopAllApp(true)
    // }

    // 热血江湖
    if (!hasExcuteFunc('热血江湖') && getExcuteFuncTimes('hangup_挂游戏times') < isEnableSwitch('moregametime') ? 5 : 3) {
        agentOrHidHome()
        sleep.millisecond(500)
        //找色返回目标数组
        if (startApp('com.rxjhxh.xh')) {
            isuccessPlay = true
            addExcuteFuncTimes('hangup_挂游戏times')

            function closeX() {
                var detectsclose = opencv.findImagesEx('图色364199.cv');
                if (detectsclose != null) {
                    agentOrHidClick(detectsclose[0])
                }
            }

            // ocrTextTrue('完成任务')
            startGame_thread(
                () => {
                    //找图返回目标数组
                    ocrWaitForText('公告', 6)
                    closeX()
                    agentOrHidClick(ocrText('开始游戏', '开始'), 3)
                    agentOrHidClick(ocrText('江湖出道', '开始'))

                }, (steptimes) => {
                    if (steptimes % 5 == 0) {
                        agentOrHidClick(ocrText('可领取', '接受任务', '完成任务', '立即前往', '【主', '主】', '点击任意'), 2, () => {
                            agentOrHidClick(ocrText('完成任务'))
                            agentOrHidClick(ocrTextMoreComplete('加入'))
                            agentOrHidClick(ocrText('点击任意'))
                        })
                    }
                    if (steptimes % 12 == 0) {
                        closeX()
                        agentOrHidClick(ocrTextMoreComplete('购买', '修炼', '配置'))
                        agentOrHidClick(ocrText('开始游戏', '开始', '江湖出道', '回城', '领取奖励', '立即前往'))
                        agentOrHidClick(ocrText('创建角色', '角色', '确定', '打开商店'))


                    }

                    if (steptimes % 24 == 0) {
                        click(0.5, 0.5)
                        //找图返回目标数组
                        var detects = opencv.findImagesEx('图色425284.cv');
                        if (detects != null) {
                            agentOrHidClick(detects[0])
                        }

                        //找图返回目标数组
                        var detects2 = opencv.findImagesEx('图色849516.cv');
                        if (detects2 != null) {
                            agentOrHidClick(detects2[0])
                        }

                        //找图返回目标数组
                        var detects3 = opencv.findImagesEx('图色494864.cv');
                        if (detects3 != null) {
                            agentOrHidClick(detects3[0])
                        }

                        //找图返回目标数组
                        var detects4 = opencv.findImagesEx('图色986307.cv');
                        if (detects4 != null) {
                            agentOrHidClick(detects4[0])
                        }

                        //找图返回目标数组
                        var detects5 = opencv.findImagesEx('图色545834.cv');
                        if (detects5 != null) {
                            agentOrHidClick(detects5[0])
                        }
                    }

                }
            )

            sleep.second((isEnableSwitch('moregametime') ? getRandomNum(30, 60) : getRandomNum(15, 20)) * 60)
            if (threadGame && threadGame.isAlive()) {
                threadGame.stop()
            }
            setExcuteFunc('热血江湖')
            setExcuteFuncInTime一段时间内不再执行('hangup_挂游戏', 9 * 60 * 60)
            stopAllApp(true)
            agentOrHidHome()
            sleep.second(1)
        }

    }

    // 原始征途
    if (!hasExcuteFunc('原始征途') && getExcuteFuncTimes('hangup_挂游戏times') < isEnableSwitch('moregametime') ? 5 : 3) {
        agentOrHidHome()
        sleep.millisecond(500)
        //找色返回目标数组
        if (startApp('com.ztgame.yszt')) {
            isuccessPlay = true
            addExcuteFuncTimes('hangup_挂游戏times')

            function closeX() {
                //找图返回目标数组
                var detectsx = opencv.findImagesEx('图色570934.cv');
                if (detectsx != null) {
                    agentOrHidClick(detectsx[0])
                }
            }

            sleep.second(3)

            // ocrTextTrue('完成任务')
            startGame_thread(
                () => {
                    click(0.5, 0.5, 2)
                    //找图返回目标数组
                    ocrWaitForText('公告', 6)
                    closeX()
                    // ocrTextTrue('踏上征途')
                    agentOrHidClick(ocrText('踏上征途', '上征途'), 3)
                    agentOrHidClick(ocrText('进入游戏', '进入游', '入游戏'))
                }, (steptimes) => {
                    if (steptimes % 5 == 0) {
                        //找图返回目标数组
                        // ocrTextMoreComplete('装备')
                        var detects手指 = opencv.findImagesEx('图色187613.cv');
                        if (detects手指 != null) {
                            if (Math.random() > 50) {
                                agentOrHidClickOffectY(detects手指[0], 15)
                            } else {
                                agentOrHidClickOffectY(detects手指[0], 5)
                            }
                        }

                        agentOrHidClick(ocrTextMoreComplete('继续任务', '使用', '替换', '确定', '挑战', '装备'))
                        agentOrHidClick(ocrText('继续任务', '开始'))
                    }
                    if (steptimes % 12 == 0) {
                        //找图返回目标数组
                        var detects = opencv.findImagesEx('图色485002.cv');
                        if (detects != null) {
                            agentOrHidClick(detects[0])
                        }
                    }

                    if (steptimes % 24 == 0) {
                        closeX()
                        agentOrHidClick(ocrText('踏上征途', '进入游戏', '进入游', '入游戏'))
                    }

                }
            )

            sleep.second((isEnableSwitch('moregametime') ? getRandomNum(30, 60) : getRandomNum(15, 20)) * 60)
            if (threadGame && threadGame.isAlive()) {
                threadGame.stop()
            }

            setExcuteFunc('原始征途')
            setExcuteFuncInTime一段时间内不再执行('hangup_挂游戏', 9 * 60 * 60)
            stopAllApp(true)
            agentOrHidHome()
            sleep.second(1)
        }

    }

    //征途2
    if (!hasExcuteFunc('征途2') && getExcuteFuncTimes('hangup_挂游戏times') < isEnableSwitch('moregametime') ? 5 : 3) {
        agentOrHidHome()
        sleep.millisecond(500)
        //找色返回目标数组
        if (startApp('com.ztgame.fir.eagle')) {
            isuccessPlay = true
            addExcuteFuncTimes('hangup_挂游戏times')

            startGame_征途2()

            sleep.second((isEnableSwitch('moregametime') ? getRandomNum(30, 60) : getRandomNum(15, 20)) * 60)
            if (thread征途2 && thread征途2.isAlive()) {
                thread征途2.stop()
            }
            setExcuteFunc('征途2')
            setExcuteFuncInTime一段时间内不再执行('hangup_挂游戏', 9 * 60 * 60)
            stopAllApp(true)
            agentOrHidHome()
            sleep.second(1)
        }



    }

    // //伏魔记
    // if (!hasExcuteFunc('伏魔记') && getExcuteFuncTimes('hangup_挂游戏times') < isEnableSwitch('moregametime') ? 5 : 3) {
    //     agentOrHidHome()
    //     sleep.millisecond(500)
    //     //找色返回目标数组
    //     if (startApp('com.fmj.bgba12')) {
    //         isuccessPlay = true
    //         addExcuteFuncTimes('hangup_挂游戏times')
    //         setExcuteFunc('伏魔记')
    //         startGame_伏魔记()

    //         sleep.second((isEnableSwitch('moregametime') ? getRandomNum(60, 90) : getRandomNum(15, 20)) * 60)
    //         if (thread伏魔记 && thread伏魔记.isAlive()) {
    //             thread伏魔记.stop()
    //         }
    //         stopAllApp(true)
    //           agentOrHidHome()
    //         sleep.second(1)
    //     }



    // }


    // for (var i = 0; i < APP_GAMES.length; i++) {
    //     if (getExcuteFuncTimes('hangup_挂游戏times') < isEnableSwitch('moregametime') ? 5 : 3) {
    //         if (startApp(APP_GAMES[i])) {

    //             sleep.second(getRandomNum(10, 15) * 60)
    //             addExcuteFuncTimes('hangup_挂游戏times')
    //         }

    //         sleep.second(1)
    //     }
    // }


    agentOrHidHome()



    stopAllApp(true)

    return isuccessPlay
}